import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/utils/user.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/widgets/loading/no_data_widget.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import '../../entity/match/expert_views_entity.dart';
import '../../entity/other/resource_entity.dart';
import '../../resources/colours.dart';
import '../../resources/styles.dart';
import '../../server/api/expert_api.dart';
import '../../server/api/other_api.dart';
import '../../utils/utils.dart';
import '../../widgets/base/qkd_text.dart';
import '../../widgets/card.dart';
import '../expert/items/plan_item.dart';
import '../home/qkd_carousel_view.dart';

class VipFreePage extends StatefulWidget {
  const VipFreePage({super.key});

  @override
  State<VipFreePage> createState() => _VipFreePageState();
}

class _VipFreePageState extends State<VipFreePage> {
  List<PlanEntity>? list;
  List<ResourceEntity> carousel = [];
  final _num = (-1.0).obs;
  final ScrollController scrollController = ApmScrollController();
  late final double topPadding = MediaQuery.of(context).padding.top + 44 + 166;

  double get num {
    if (_num < 0) {
      return 0.0;
    } else if (_num > 1) {
      return 1.0;
    } else {
      return _num.value;
    }
  }

  set num(value) {
    _num.value = value;
    update();
  }

  Future getPlan() async {
    list = await ExpertApi.getHotPlan(1, 500, 7, 0);
    setState(() {});
  }

  Future getData() async {
    carousel = [];
    var list = await OtherApi.getAppList("app_lbt");
    list?.forEach((element) {
      if (element.button == "vip_free_plan_${User.info?.isVip ?? 0}") {
        carousel.add(element);
      }
    });
    setState(() {});
  }

  @override
  void initState() {
    getData();
    getPlan();
    scrollController.addListener(() {
      setState(() {
        num = scrollController.offset / 146;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colours.greyF2F3F7,
        extendBodyBehindAppBar: true,
        appBar: Styles.appBar(
            toolbarHeight: 44,
            backgroundColor: Colours.transparent,
            title: QkdText("会员免费专区")),
        body: Stack(
          children: [
            Positioned(
              left: 0,
              right: 0,
              top: 0,
              child: QkdCarouselView(
                carousels: carousel,
                aspectRatio: 375 / 260,
                radius: 0,
                padding: 0,
                dotBottom: 20,
                needTitle: false,
                needDot: carousel.length != 1,
              ),
            ),
            Positioned.fill(
                child: SafeArea(
              child: Container(
                color: Colours.greyF2F3F7.withOpacity(num),
              ),
            )),
            SafeArea(
              child: EasyRefresh(
                onRefresh: getPlan,
                header:
                    Styles.refreshHeader(position: IndicatorPosition.locator),
                child: CustomScrollView(
                  controller: scrollController,
                  slivers: [
                    SliverToBoxAdapter(
                        child: Container(height: 166).tap(() {
                      // if(Get.currentRoute == Routes.navigation){
                      //   Get.find<NavigationLogic>().currentRoute();
                      // }
                      if (carousel.isNotEmpty) {
                        Utils.doJump(carousel.first.href ?? "");
                      }
                    })),
                    const HeaderLocator.sliver(),
                    if (list?.hasValue == true) ...[
                      ...List<Widget>.generate(
                          list?.length ?? 0,
                          (i) => SliverToBoxAdapter(
                                child: Block(
                                  cardStyle: const CardStyle(
                                      margin: EdgeInsets.fromLTRB(8, 0, 8, 8),
                                      padding:
                                          EdgeInsets.symmetric(horizontal: 12)),
                                  child: PlanItem(tabId: 1, entity: list![i]),
                                ),
                              ))
                    ] else ...[
                      SliverLayoutBuilder(
                        builder: (context, cons) => SliverToBoxAdapter(
                          child: Container(
                            margin: const EdgeInsets.symmetric(horizontal: 8),
                            height: cons.viewportMainAxisExtent,
                            clipBehavior: Clip.antiAlias,
                            decoration: const BoxDecoration(
                                color: Colours.white,
                                borderRadius: BorderRadius.vertical(
                                    top: Radius.circular(10))),
                            child: const NoDataWidget(tip: "暂无相关方案")
                                .paddingOnly(bottom: 220),
                          ),
                        ),
                      )
                    ]
                  ],
                ),
              ),
            )
          ],
        ));
  }
}
